package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.supplier.entity.SEvaluationPerformScores;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSEvaluationPerformScores;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSEvaluationPerson;
import com.movitech.mobile.cz.base.supplier.mapper.SEvaluationPerformScoresMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSEvaluationPerformScoresMapper;
import com.movitech.mobile.cz.base.supplier.service.SEvaluationPerformScoresService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 供方评估--履约评分 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sEvaluationPerformScoresService")
@Transactional
public class SEvaluationPerformScoresServiceImpl extends ServiceImpl<SEvaluationPerformScoresMapper, SEvaluationPerformScores> implements SEvaluationPerformScoresService {

    @Autowired
    private SEvaluationPerformScoresMapper sEvaluationPerformScoresMapper;
    
    @Autowired
    private ExtSEvaluationPerformScoresMapper extSEvaluationPerformScoresMapper;


    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = { "RuntimeException", "Exception" })
    @Override
    public boolean syncSEvaluationPerformScores(String evaluationId, List<ExtSEvaluationPerformScores> sEvaluationPerformScores, List<ExtSEvaluationPerson> extSEvaluationPeople) {
        Integer a = extSEvaluationPerformScoresMapper.getMaxBatchCodeByEvaluationId(evaluationId);
    	int batchCode = a==null?0:a;
    	batchCode = batchCode + 1;
        String userId = UserUtils.getCurrentUserId();
    	if(sEvaluationPerformScores != null){
            SEvaluationPerformScores where = new SEvaluationPerformScores();
            where.setEvaluationId(evaluationId);
            EntityWrapper entityWrapper = new EntityWrapper();
            entityWrapper.setEntity(where);
            SEvaluationPerformScores update = new SEvaluationPerformScores();
            update.setDelFlag(CommonConstants.DEL_FLAG_DELETE);
            update(update,entityWrapper);
            int i = 0;
            for(SEvaluationPerformScores template : sEvaluationPerformScores){
                template.setEvaluationId(evaluationId);
                template.setBatchCode(batchCode);
                template.setBaseFeilds(userId, null, CommonConstants.NO_INTEGER, true);
                for(ExtSEvaluationPerson extSEvaluationPerson:extSEvaluationPeople){
                    if(extSEvaluationPerson.getDepartmentId().equals(template.getEvaluationDepartmentId())){
                        template.setEvaluatorUserId(extSEvaluationPerson.getUserId());
                        break;
                    }
                }
                i =+ sEvaluationPerformScoresMapper.insert(template);
            }
            return i == sEvaluationPerformScores.size();
        }
        return true;
    }
}
